// 导入API
import { login, regisgter,logout } from '../../api/account/login'
import { getCollects } from '../../api/account/collects'
import * as types from '../types'
import {avatorUpload} from '../../api/account/avator'
import { stat } from 'fs';
export default {
    namespaced:true,
    state: {
        login: false,
        username: '',
        collectList:[],
        buyLlist:[],
        avatar:''
    },
    mutations: {
        [types.SET_USER](state, username) {
            // 修改用户信息
            //设置store
            state.username = username
        },
        [types.GET_USER](state) {
            return state.username 
        },
        [types.LOGIN](state, username) {
            // 登陆 
            state.login = true
            // 设置用户信息
        },
        [types.LOGOUT](state) {
            //退出登陆 设置state
            state.login = false
            state.username = ''
            state.collectList = []
            state.buyLlist = []
            state.avatar =''
            // 清空localStorage
            localStorage.clear()
        },
        [types.SET_COLLECTS](state,list) {
            // 设置收藏列表
            state.collectList = list
        },
        // 设置头像
        [types.SET_AVATAR](state,avatar){
            state.avatar = avatar
        }
    },
    actions: {
        async [types.TO_LOGIN]({ commit }, context) {
            // 获取邮箱和密码
            let { email, password } = context
            // 登陆
            let res = await login(email, password);
            if (res.code === 0) {
                // 登陆成功
                commit(types.LOGIN, res.data.username)
                commit(types.SET_USER, res.data.username)
                commit(types.SET_AVATAR, res.data.avatar)
                return res.msg
            } else {
                // 登录失败
                return Promise.reject(res.data)
            }
        },
        async [types.TO_REGISTER ]({ commit }, context) {
            // 获取 用户名 邮箱 密码
            let { username, email, password } = context
            // 注册
            let res = await regisgter(username, email, password)
            // 判断 注册是否成功
            if (res.code === 0) {
                return res.msg
            } else {
                return Promise.reject(res.data)
            }
        },
        async [types.TO_LOGOUT]({ commit }, context) {
            let res = await logout()
            if (res.code === 0) {
                // 此时已经删除cookie
                commit(types.LOGOUT)
                // 清课程列表
                commit("course/"+types.SET_ORDER_COURSE,[],{root:true})
                return res.msg
            } else {
                return Promise.reject(res.data)
            }
        },
        async [types.GET_COLLECTS]({ commit }, context) {
            let res = await getCollects()
            if (res.code === 0) {
                // 此时已经删除cookie
                commit(types.SET_COLLECTS,res.data)
            } else {
                return Promise.reject(res.data)
            }
        },
        async [types.UPLOAD_AVATOR]({commit},context){
            let res= await avatorUpload(context)
            if(res.code === 0 ){
                // 更新头像
                commit(types.SET_AVATAR,res.data)
            }else{
                return Promise.reject(res.data)
            } 
        }
    }
}